package com.bibrApp.difu.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bibrApp.difu.R;


/**
 * Created by Administrator on 2016/11/3.
 */

public class DFDialog extends Dialog {

    private static TextView tvTitle;
    private static TextView tvContent;
    private static TextView btNagetive;
    private static TextView btPositive;
    private static TextView btSure;

    public DFDialog(Context context) {
        super(context);
    }

    public DFDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected DFDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private String title;
        private CharSequence message;
        private String positiveButtonText;
        private String negativeButtonText;
        boolean cancel = true;
        boolean autoDismiss = true;
        private View contentView;
        private int titleVisible = View.VISIBLE;
        private int positiveVisible = View.VISIBLE;
        private boolean isSure = false;
        private boolean isDissMiss = false;
        private boolean isTwoBtnGone = false;

        public Builder setTwoBtnGone(boolean twoBtnGone) {
            isTwoBtnGone = twoBtnGone;
            return this;
        }

        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private LinearLayout twoBtnLL;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setDissMiss(boolean dissMiss) {
            isDissMiss = dissMiss;
            return this;
        }

        public Builder setMessage(CharSequence message) {
            this.message = message;
            return this;
        }

        public Builder setCancel(boolean cancel) {
            this.cancel = cancel;
            return this;
        }

        public Builder setSure(boolean isSure) {
            this.isSure = isSure;
            return this;
        }

        public Builder autoDismiss(boolean flag) {
            this.autoDismiss = flag;
            return this;
        }

        public Builder setPositiveVisible(int positiveVisible) {
            this.positiveVisible = positiveVisible;
            return this;
        }

        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        public Builder setTitleVisible(int visible) {
            this.titleVisible = visible;
            return this;
        }

        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(int positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public DFDialog show() {
            DFDialog dialog = create();
            dialog.show();
            return dialog;
        }

        public DFDialog create() {
            // instantiate the dialog with the custom Theme
            final DFDialog dialog = new DFDialog(context, R.style.Dialog);
            contentView = View.inflate(context, R.layout.dialog, null);
            dialog.addContentView(contentView, new ViewGroup.LayoutParams(-1, -2));
            if (isDissMiss) {
                dialog.setCanceledOnTouchOutside(false);//点击其他区域不可关闭弹窗
            }
            tvTitle = (TextView) contentView.findViewById(R.id.dialog_title);
            tvContent = (TextView) contentView.findViewById(R.id.dialog_content);
            btNagetive = contentView.findViewById(R.id.dialog_nagetive);
            btPositive = contentView.findViewById(R.id.dialog_positive);
            btSure = contentView.findViewById(R.id.dialog_sure);
            twoBtnLL = contentView.findViewById(R.id.dialog_two_btn);

            if(isTwoBtnGone){
                twoBtnLL.setVisibility(View.GONE);
                btSure.setVisibility(View.GONE);
            }

            tvTitle.setText(title);
            tvTitle.setVisibility(titleVisible);

            btSure.setVisibility(isSure ? View.VISIBLE : View.INVISIBLE);
            btPositive.setVisibility(isSure ? View.INVISIBLE : View.VISIBLE);
            btNagetive.setVisibility(isSure ? View.INVISIBLE : View.VISIBLE);

            if (isSure) {
                btSure.setText(positiveButtonText);
                btSure.setOnClickListener(v -> {
                    if (positiveButtonClickListener != null) {
                        positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    }
                    if (autoDismiss) {
                        dialog.dismiss();
                    }
                });
            }

            if (!TextUtils.isEmpty(positiveButtonText)) {
                btPositive.setText(positiveButtonText);
                btPositive.setOnClickListener(v -> {
                    if (positiveButtonClickListener != null) {
                        positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    }
                    if (autoDismiss) {
                        dialog.dismiss();
                    }
                });
            } else {
                btPositive.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(negativeButtonText)) {
                btNagetive.setText(negativeButtonText);
                btNagetive.setOnClickListener(v -> {
                    if (negativeButtonClickListener != null) {
                        negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                    }
                    if (autoDismiss) {
                        dialog.dismiss();
                    }

                });
            } else {
                btNagetive.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(message)) {
                tvContent.setText(message);
            }else{
                tvContent.setVisibility(View.GONE);
            }

            contentView.invalidate();
            dialog.setContentView(contentView);
            dialog.setCancelable(cancel);
            return dialog;
        }
    }
}
